using System;
using Server;
using Server.Mobiles;
using System.Collections.Generic;

namespace Server.Engines.Quests.RandomQuests
{
    public class RandomQuest : QuestSystem
	{

		private static Type[] m_TypeReferenceTable = new Type[]
			{
				typeof( QuestAcceptConversation ),
				typeof( RandomQuestKillObjective ),
				typeof( RandomQuestFindObjective ),
				typeof( ReturnToQuestgiverObjective ),
				typeof( QuestgiverThanksConversation ),
			};

		public override Type[] TypeReferenceTable{ get{ return m_TypeReferenceTable; } }

		public override object Name
		{
			get
			{
				return m_Name;
			}
		}

		public override object OfferMessage
		{
			get
			{
				return m_Story;
			}
		}

		public override TimeSpan RestartDelay{ get{ return TimeSpan.FromHours(1); } }
		public override bool IsTutorial{ get{ return false; } }
		public override int Picture { get { return 0x15B5; } }

        private string m_Name;
        private string m_Story;
        private int m_Level;
        public int Level { get { return m_Level+1; } }
        private RandomQuestType m_Type;

		private RandomQuestGiver m_Giver;
		public RandomQuestGiver Giver { get { return m_Giver; } set { m_Giver = value; } }

        private MonsterTypeName m_Monster;
        public MonsterTypeName Monster { get { return m_Monster; } set { m_Monster = value; } }


		public RandomQuest( PlayerMobile from, RandomQuestGiver giver, int level, RandomQuestType questtype )
			: base( from )
		{
            m_Level = level;
            m_Type = questtype;
			m_Giver = giver;

            if (m_Type == RandomQuestType.Find)
            {
                int length = Utility.RandomMinMax(1, 100);

                m_Name = "Seek ";
                if (length > 90)
                {
                    m_Name += RandomQuestDefines.Items5[Utility.Random(RandomQuestDefines.Items5.Length)] + "'s Scroll of " + RandomQuestDefines.Items6[Utility.Random(RandomQuestDefines.Items6.Length)] + " " + RandomQuestDefines.Items7[Utility.Random(RandomQuestDefines.Items7.Length)];
                }
                else if (length > 45)
                {
                    m_Name += "the " + RandomQuestDefines.Items3[Utility.Random(RandomQuestDefines.Items3.Length)] + " " + RandomQuestDefines.Items1[Utility.Random(RandomQuestDefines.Items1.Length)] + " of " + RandomQuestDefines.Items4[Utility.Random(RandomQuestDefines.Items4.Length)];
                }
                else
                {
                    m_Name += "the " + RandomQuestDefines.Items1[Utility.Random(RandomQuestDefines.Items1.Length)] + " of " + RandomQuestDefines.Items2[Utility.Random(RandomQuestDefines.Items2.Length)];
                }
                m_Story = RandomQuestDefines.Story1[Utility.Random(RandomQuestDefines.Story1.Length)] + " " + RandomQuestDefines.Places[m_Level][Utility.Random(RandomQuestDefines.Places[m_Level].Count)];
            }
            else
            {
                Monster = RandomQuestDefines.Monsters[m_Level][Utility.Random(RandomQuestDefines.Monsters[m_Level].Count)];
                m_Name = "Slay a " + Monster.Name;

                string person = "";

                if (Utility.RandomBool())
                    person = NameList.RandomName("female");
                else
                    person = NameList.RandomName("male");

                if (Utility.RandomMinMax(1, 3) > 1)
                    m_Story = person + " " + RandomQuestDefines.Story2[Utility.Random(RandomQuestDefines.Story2.Length)];
                else
                    m_Story = RandomQuestDefines.Story3[Utility.Random(RandomQuestDefines.Story3.Length)] + " " + person;
            }
		}

		// Serialization
		public RandomQuest()
		{
		}

		public override void ChildDeserialize( GenericReader reader )
		{
			int version = reader.ReadInt();

			m_Giver = (RandomQuestGiver)reader.ReadMobile();
            m_Level = reader.ReadInt();
            m_Type = (RandomQuestType)reader.ReadInt();
            m_Name = reader.ReadString();
            m_Story = reader.ReadString();
		}

		public override void ChildSerialize( GenericWriter writer )
		{
			writer.Write( (int)0 ); // version

			writer.Write( (Mobile)m_Giver );
            writer.Write(m_Level);
            writer.Write((int)m_Type);
            writer.Write(m_Name);
            writer.Write(m_Story);
		}

		public void CheckComplete()
		{
			bool complete = true;
			for ( int i = 0; i < Objectives.Count; i++ )
			{
				if ( !((QuestObjective)Objectives[i]).Completed )
				{
					complete = false;
					break;
				}
			}
			if ( complete )
			{
				AddObjective( new ReturnToQuestgiverObjective() );
			}
		}

		public override void Accept()
		{
			if ( m_Giver != null )
			{
				m_Giver.PlaySound( 0x431 );

				base.Accept();
                if (m_Type == RandomQuestType.Kill)
                {
                    RandomMonster monster = new RandomMonster(Monster.Name, Monster.Types, Math.Max(1, Utility.Random(25 - m_Level * 5)));
                    AddObjective(new RandomQuestKillObjective(monster));
                }
                else
                    AddObjective(new RandomQuestFindObjective());
            }
		}
	}
}